/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.maxato.services;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import nl.maxato.domain.Athlete;
import nl.maxato.domain.BestTeamPerDiscipline;
import nl.maxato.domain.WeeklyTest;
import nl.maxato.xmlservices.ConnectToURL;
import nl.maxato.xmlservices.ConvertXMLData;
import nl.maxato.xmlservices.GetXMLData;
import nl.maxato.xmlservices.XMLService;

/**
 *
 * @author Danny
 */
public class CalculateBestTeam {

	XMLService xmlService;
	WeeklyTest wt;

    public CalculateBestTeam() {
        wt = new WeeklyTest();
        wts = new ArrayList<Athlete>();
        xmlService = XMLService.getInstance();
        getWeeklyTestofAthlete();
        
        //cxd.weeklyTestDataOmzetten();
        //calculateForThreeEvents();
    }
    ArrayList<Athlete> wts;
    ArrayList<WeeklyTest> wtsAll;

    private void getWeeklyTestofAthlete() {
        String cookie = xmlService.signIn("Gynnad", "1q2w3e4r");
		xmlService.getData(cookie, "weeklytest");
		//gxd.getStringData(cookie, Integer.toString(id));
		wt = xmlService.weeklyTestDataOmzetten();
		//wts.add(cxd.weeklyTestDataOmzetten(id));


    }
    
    public int getEventScoreOfAthleteByEventAndId(int id, int event){
        for (int i = 0; i < wt.getAthletesArrayList().size(); i++) {
            if (wt.getAthletesArrayList().get(i).getIdOfAthlete() == id) {
                for (int j = 0; j < wt.getAthletesArrayList().get(i).getEventsArrayList().size(); j++) {
                    if (wt.getAthletesArrayList().get(i).getEventsArrayList().get(j).getIdEvent() == event) {
                        return wt.getAthletesArrayList().get(i).getEventsArrayList().get(j).getWeeksEvent().get(0).getScoreEvent();
                    }
                }
            }
        }
        //WeeklyTest wtts = cxd.weeklyTestDataOmzetten(id);
        return 0;//wtts.getEventsWeeklyTest().get(0).getWeeksEvent().get(event).getScoreEvent();
        
    }
    int events = 3;
    int athletes = 6;
    BestTeamPerDiscipline btpd;

    public BestTeamPerDiscipline calculateForThreeEvents(ArrayList<Athlete> athletes) {
        wts = new ArrayList<Athlete>();
        btpd = null;
        //Atleet weeklytest ophalen
        if (athletes.size() <= 2) {
            return btpd = new BestTeamPerDiscipline(0, 0, 0, 0, 0, 0);
        }
        for (int i = 0; i < wt.getAthletesArrayList().size(); i++) {
            for (int j = 0; j < athletes.size(); j++) {
                if (wt.getAthletesArrayList().get(i).getIdOfAthlete() == athletes.get(j).getIdOfAthlete()) {
                    wts.add(wt.getAthletesArrayList().get(i));
                }
            }
       }
        int best = 0;
        int result = 0;
        //per atleet gaan kijken wat de score is
        for (int i = 0; i < wts.size(); i++) {
            for (int j = 0; j < wts.size(); j++) {
                for (int k = 0; k < wts.size(); k++) {
                    //kijken je niet dezelfde atleet hebt
                    if (wts.get(i) == wts.get(j) || wts.get(k) == wts.get(j) || wts.get(k) == wts.get(i)) {
                    } else {
                        result = wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent()
                                + wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent()
                                + wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent();
                    }
                    //Beste resultaat bewaren
                    if (best < result) {
                        best = result;
                        //resultaat toevoegen aan btpd, zodat je het AtleetID hebt en de score van die atleet op dat onderdeel
                        btpd = new BestTeamPerDiscipline(
                                wts.get(i).getIdOfAthlete(), wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent(),
                                wts.get(j).getIdOfAthlete(), wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent(),
                                wts.get(k).getIdOfAthlete(), wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent());
                    }
                }

            }

        }
        return btpd;
    }

    public BestTeamPerDiscipline calculateForFourEvents(ArrayList<Athlete> athletes) {
        wts = new ArrayList<Athlete>();
        btpd = null;
        //Atleet weeklytest ophalen
        if (athletes.size() <= 3) {
            return btpd = new BestTeamPerDiscipline(0, 0, 0, 0, 0, 0);
        }
        for (int i = 0; i < wt.getAthletesArrayList().size(); i++) {
            for (int j = 0; j < athletes.size(); j++) {
                if (wt.getAthletesArrayList().get(i).getIdOfAthlete() == athletes.get(j).getIdOfAthlete()) {
                    wts.add(wt.getAthletesArrayList().get(i));
                }
            }
       }
        int best = 0;
        int result = 0;
        //per atleet gaan kijken wat de score is
        for (int i = 0; i < wts.size(); i++) {
            for (int j = 0; j < wts.size(); j++) {
                for (int k = 0; k < wts.size(); k++) {
                    for (int l = 0; l < wts.size(); l++) {
                        //kijken je niet dezelfde atleet hebt
                        if (wts.get(i) == wts.get(j) || wts.get(k) == wts.get(j) || wts.get(k) == wts.get(i) || wts.get(l) == wts.get(i) || wts.get(l) == wts.get(j) || wts.get(l) == wts.get(k)) {
                        } else {
                            result = wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent()
                                    + wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent()
                                    + wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent()
                                    + wts.get(l).getEventsArrayList().get(3).getWeeksEvent().get(0).getScoreEvent();
                        }
                        //Beste resultaat bewaren
                        if (best < result) {
                            best = result;
                            //resultaat toevoegen aan btpd, zodat je het AtleetID hebt en de score van die atleet op dat onderdeel
                            btpd = new BestTeamPerDiscipline(
                                    wts.get(i).getIdOfAthlete(), wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent(),
                                    wts.get(j).getIdOfAthlete(), wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent(),
                                    wts.get(k).getIdOfAthlete(), wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent(),
                                    wts.get(l).getIdOfAthlete(), wts.get(l).getEventsArrayList().get(3).getWeeksEvent().get(0).getScoreEvent());
                        }
                    }
                }

            }

        }
        return btpd;
    }

    public BestTeamPerDiscipline calculateForFiveEvents(ArrayList<Athlete> athletes) {
        wts = new ArrayList<Athlete>();
        btpd = null;
        //Atleet weeklytest ophalen
        if (athletes.size() <= 4) {
            return btpd = new BestTeamPerDiscipline(0, 0, 0, 0, 0, 0);
        }
        for (int i = 0; i < wt.getAthletesArrayList().size(); i++) {
            for (int j = 0; j < athletes.size(); j++) {
                if (wt.getAthletesArrayList().get(i).getIdOfAthlete() == athletes.get(j).getIdOfAthlete()) {
                    wts.add(wt.getAthletesArrayList().get(i));
                }
            }
       }
        int best = 0;
        int result = 0;
        //per atleet gaan kijken wat de score is
        for (int i = 0; i < wts.size(); i++) {
            for (int j = 0; j < wts.size(); j++) {
                for (int k = 0; k < wts.size(); k++) {
                    for (int l = 0; l < wts.size(); l++) {
                        for (int m = 0; m < wts.size(); m++) {
                            //kijken je niet dezelfde atleet hebt
                            if (wts.get(i) == wts.get(j) || wts.get(k) == wts.get(j)
                                    || wts.get(k) == wts.get(i) || wts.get(l) == wts.get(i)
                                    || wts.get(l) == wts.get(j) || wts.get(l) == wts.get(k)
                                    || wts.get(m) == wts.get(k) || wts.get(l) == wts.get(m)
                                    || wts.get(i) == wts.get(m) || wts.get(j) == wts.get(m)) {
                            } else {
                                result = wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent()
                                        + wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent()
                                        + wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent()
                                        + wts.get(l).getEventsArrayList().get(3).getWeeksEvent().get(0).getScoreEvent()
                                        + wts.get(m).getEventsArrayList().get(4).getWeeksEvent().get(0).getScoreEvent();
                            }
                            //Beste resultaat bewaren
                            if (best < result) {
                                best = result;
                                //resultaat toevoegen aan btpd, zodat je het AtleetID hebt en de score van die atleet op dat onderdeel
                                btpd = new BestTeamPerDiscipline(
                                        wts.get(i).getIdOfAthlete(), wts.get(i).getEventsArrayList().get(0).getWeeksEvent().get(0).getScoreEvent(),
                                        wts.get(j).getIdOfAthlete(), wts.get(j).getEventsArrayList().get(1).getWeeksEvent().get(0).getScoreEvent(),
                                        wts.get(k).getIdOfAthlete(), wts.get(k).getEventsArrayList().get(2).getWeeksEvent().get(0).getScoreEvent(),
                                        wts.get(l).getIdOfAthlete(), wts.get(l).getEventsArrayList().get(3).getWeeksEvent().get(0).getScoreEvent(),
                                        wts.get(m).getIdOfAthlete(), wts.get(l).getEventsArrayList().get(4).getWeeksEvent().get(0).getScoreEvent());
                            }
                        }
                    }
                }

            }

        }
        return btpd;
    }
}
